<template>
  <div class="playbox">
    <!-- 播放器 -->
    <div class="play">
      <!-- 封图 -->
      <div
        class="photo rotate"
        v-if="picUrl !== ''"
        :style="{ animationPlayState: isPaused ? 'paused' : 'running' }"
        @click="showPlayPage"
      >
        <img :src="picUrl" alt="" />
      </div>

      <div class="photo" v-else @click="showPlayPage">
        <img src="../assets/logo.svg" alt="" />
      </div>

      <!-- 名字 -->
      <div class="playname">
        <div class="song">{{ songName }}</div>
        <div class="songer" @click="viewSonger(singerId)">
          {{ songerName }}
        </div>
      </div>

      <!-- 播放 -->
      <div class="playbox" @click="playAndStop">
        <van-circle
          v-model="currentRate"
          :rate="rate"
          color="#f12a24"
          size="30"
          stroke-width="160"
        >
          <van-icon :name="isPaused ? 'play-circle-o' : 'pause-circle-o'" />
        </van-circle>
      </div>

      <!-- 列表 -->
      <div class="playlist" @click="show = true">
        <van-icon name="bars" />
      </div>
    </div>

    <!-- 播放歌曲列表 -->
    <div class="songlist">
      <van-popup v-model="show">
        <div class="playtit">播放列表</div>

        <div v-if="defaultSongList.length === 0 ? true : false">
          <van-empty description="列表空空，快去听歌吧" />
        </div>

        <!-- 歌曲列表 -->
        <div
          class="tabbox"
          v-for="(item, index) in defaultSongList"
          :key="index"
          v-else
          @click="
            playAudio(
              item.songId,
              item.picUrl,
              item.songName,
              item.singerName,
              item.singerId
            )
          "
        >
          <!-- 封图 -->
          <div class="sealing">
            <van-image :src="item.picUrl" lazy-load>
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
            </van-image>
          </div>

          <!-- 名字 -->
          <div class="tabname">
            <div class="tabsongname">{{ item.songName }}</div>
            <div class="tabtxt">
              <div class="textbox">SQ</div>
              {{ item.singerName }}
            </div>
          </div>

          <div class="tabplay">
            <van-icon
              :name="
                audioId !== item.songId
                  ? 'play-circle-o'
                  : isPaused
                  ? 'play-circle-o'
                  : 'pause-circle-o'
              "
            />
          </div>

          <div class="share" @click="showShare = true">
            <van-icon name="ellipsis" />
          </div>
        </div>

        <!-- 分享面板 -->
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "Playbox",

  data() {
    return {
      currentRate: 0,
      
      gradientColor: {
        "0%": "#3fecff",
        "100%": "#6149f6",
      },

      show: false,

      // 分享面板
      showShare: false,
      options: [
        [
          { name: "微信", icon: "wechat" },
          { name: "朋友圈", icon: "wechat-moments" },
          { name: "微博", icon: "weibo" },
          { name: "QQ", icon: "qq" },
        ],
        [
          { name: "复制链接", icon: "link" },
          { name: "分享海报", icon: "poster" },
          { name: "二维码", icon: "qrcode" },
          { name: "小程序码", icon: "weapp-qrcode" },
        ],
      ],
    };
  },

  computed: {
    ...mapState([
      "picUrl",
      "isPaused",
      "audioEle",
      "songName",
      "songerName",
      "singerId",
      "defaultSongList",
      "audioId",
      "rate"
    ]),
  },

  methods: {
    //查看歌手页面
    viewSonger(id) {
      this.$router.push({ name: "Songer", params: { id } });
    },

    ...mapMutations(["changeStateProperty"]),

    //播放、暂停
    playAndStop() {
      if (this.isPaused) {
        //播放音频
        this.audioEle.play();
        this.changeStateProperty({ key: "isPaused", value: false });
      } else {
        //暂停音频
        this.audioEle.pause();
        this.changeStateProperty({ key: "isPaused", value: true });
      }
    },

    //显示播放主页
    showPlayPage() {
      this.changeStateProperty({ key: "isShowPlayPage", value: true });
    },

    // 点击播放音频
    playAudio(id, picUrl, songName, songerName, singerId) {
      if (id === this.audioId) {
        if (!this.audioEle.paused) {
          //暂停歌曲
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        } else {
          //播放歌曲
          this.audioEle.play();
          this.changeStateProperty({ key: "isPaused", value: false });
        }
        return;
      }

      //获取音频url
      this.axios({
        method: "get",
        url: "/song/url",
        params: {
          id,
        },
      })
        .then((res) => {
          let audioUrl = res.data.data[0].url;
          this.changeStateProperty({ key: "audioUrl", value: audioUrl });
          this.changeStateProperty({ key: "audioId", value: id });
          //获取暂停状态
          this.changeStateProperty({ key: "isPaused", value: false });
        })
        .catch((err) => {
          
        });

      //获取播放图片
      this.changeStateProperty({ key: "picUrl", value: picUrl });

      this.changeStateProperty({ key: "songName", value: songName });
      this.changeStateProperty({ key: "songerName", value: songerName });
      this.changeStateProperty({ key: "singerId", value: singerId });

      //获取专辑评论数据
      this.axios({
        method: "get",
        url: "/comment/music",
        params: {
          id,
        },
      })
        .then((res) => {
          this.changeStateProperty({
            key: "audioComment",
            value: res.data.hotComments,
          });
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
//播放器
.play {
  width: 100%;
  height: 40px;
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding: 0px 14px;
}

.photo {
  width: 50px;
  height: 50px;
  background: url("../assets/Blackglue.png") no-repeat center;
  background-size: cover;
  border-radius: 50%;
  position: relative;
  bottom: 10px;
}

.photo img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  position: relative;
  top: 10px;
  left: 8px;
}

.playname {
  width: 70%;
  display: flex;
  padding-left: 20px;
  align-items: center;
}

.song {
  max-width: 130px;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.songer {
  max-width: 80px;
  font-size: 12px;
  padding-left: 10px;
  padding-top: 3px;
  transform: scale(0.9);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.playbox {
  padding-right: 14px;
  font-size: 26px;
  &/deep/.van-circle {
    top: 2px;
  }
  &/deep/.van-icon {
    top: 1px;
  }
}

.playlist {
  font-size: 26px;
  &/deep/.van-icon {
    bottom: 0;
  }
}

//播放列表
.songlist {
  &/deep/.van-popup--center {
    top: 64%;
    width: 90%;
    height: 64%;
    padding: 10px 20px;
    background-color: #fff;
    box-sizing: border-box;
    border-radius: 14px;
  }
}

.playtit {
  width: 100%;
  height: 30px;
  font-size: 18px;
  font-weight: 600;
}

//歌曲列表
.tabbox {
  width: 100%;
  display: flex;
  align-items: center;
  margin-top: 10px;
  &/deep/.van-icon {
    left: 20px;
    bottom: 6px;
  }
}

.sealing {
  width: 40px;
  height: 40px;
}

.sealing {
  &/deep/ .van-image__img {
    width: 40px;
    height: 40px;
  }
}

.tabname {
  width: 58%;
  display: flex;
  flex-wrap: wrap;
  padding-left: 6px;
}

.tabsongname {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding-left: 10px;
}

.tabtxt {
  display: flex;
  font-size: 12px;
  color: #9f9f9f;
  align-items: center;
}

.textbox {
  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid red;
  color: red;
  transform: scale(0.5);
}

.tabplay {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 20px;
}

.share {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 10px;
}
</style>